<template>
    <el-dialog :visible="visible" :center="true" width="460px" :before-close="handleClose">
        <span slot="title">{{ type == 'edit' ? '修改角色' : '添加角色' }}</span>
        <el-form :model="ruleForm" :rules="rules" :label-position="labelPosition" ref="ruleForm" label-width="100px"
            class="demo-ruleForm">
            <el-form-item label="角色名称" prop="roleName">
                <el-input v-model="ruleForm.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDesc">
                <el-input type="textarea" maxlength="30" show-word-limit v-model="ruleForm.roleDesc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { AddRoles, editRoles } from '@/api/http';
export default {
    name: 'Dialog',
    props: { visible: Boolean, type: String, userInfo: Object },
    data() {
        return {
            ruleForm: Object.assign({}, this.userInfo),
            labelPosition: 'right',
            rules: {
                rolesName: [
                    { required: true, message: '请输入角色名称', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    watch: {
        visible: {
            // immediate: true,
            handler(val) {
                val ? this.ruleForm = Object.assign(this.userInfo) : this.ruleForm = {}
            }
        }
    },
    methods: {
        // 关闭弹窗
        handleClose() {
            this.updateVisible(false)
        },
        // 提交
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.type == 'edit') {
                        this.EditRoles()
                    } else {
                        this.addRoles()
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 添加用户
        async addRoles() {
            const { data, meta } = await AddRoles(this.ruleForm)
            if (meta.status == 201) {
                this.$message.success(this.$t('message.updeteSuccess'))
                this.updateVisible(false)
                this.$emit('done')
            }
        },
        // 编辑用户
        async EditRoles() {
            const { data, meta } = await editRoles(this.userInfo.id, this.ruleForm)
            if (meta.status == 200) {
                this.$message.success(this.$t('message.updeteSuccess'))
                this.updateVisible(false)
                this.$emit('done')
            }
        },
        // 重置表格
        resetForm(formName) {
            this.ruleForm = {}
            this.$refs[formName].resetFields();
        },
        /* 更新visible */
        updateVisible(value) {
            this.$emit("update:visible", value);
        },
    },
}
</script>

<style lang="scss" scoped>
</style>